// pages/home/home.js
const app = getApp()
const config = require('../../config/config');
const api = require('../../utils/api.js')
const util = require('../../utils/util.js')
const login = require('../../utils/login.js')
const storage = require('../../utils/cache.js')
Page({
  data:{
    isAllScreen: config.isAllScreen
  },
  /**
   * 获取用户信息
   */
  getUserInfo () {
    login.login().then(res => {
      if (res.error == 0) {
        this.toGift()
      } else {
        util.showFail(res.msg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    util.checkOpenid().then((res) => {
      if (res === "true") {
        this.setData({
          haveOpenid: true
        })
      } else {
        this.setData({
          haveOpenid: false
        })
      }
    })
  },

  // 定制礼包
  toGift() {
    storage.get('openid').then(res=>{
      if (res.errMsg === "getStorage:ok"){
        let data = {
          openid: res.data
        }
        api.POST('user/makeGift', data).then(res => {
          if (res.error == 0) {
            wx.navigateTo({
              url: '../spring/spring',
            })
            app.globalData.gift = res.data.params
            app.globalData.is_pay = res.data.is_pay
            storage.put('giftId',res.data.id)
            wx.setStorageSync('proid', res.data.id)
          } else {
            util.showFail(res.msg)
          }
        })
      }
    })
  },
  onShareAppMessage() {
    return {
      title: '一起来制作2019新年大礼包~',
      path: '/pages/home/home',
      imageUrl: '../../img/share.png',
    }
  },
})